/**
 * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
 * This file is part of CSipSimple.
 *
 *  CSipSimple is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  If you own a pjsip commercial license you can also redistribute it
 *  and/or modify it under the terms of the GNU Lesser General Public License
 *  as an android library.
 *
 *  CSipSimple is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CSipSimple.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This file and this file only is also released under Apache license as an API file
 */

package com.cloudptt.api.pjsip.api;

import android.os.Parcel;
import android.os.Parcelable;

import com.mcptt.config.body.info.mcdata.binary.BinaryBody;
import com.mcptt.db.model.SessionInfo;

public class MessageInfo implements Parcelable {
    private static byte DELIVERY = 0x1;
    private static byte READ = 0x2;
    private byte signalType = 0;
    private String conversationId = null;
    private String msgId = null;

    private String msgType = null;
    private int contentType = 0;
    private String content = null;
    private String fileMetaData = null;

    private long date =  0;

    private byte status = 0;
    private int dispositionReqType = DELIVERY+READ;

    private String controllerPsi = null;//只有收消息有值 供回执使用

    private String senderFuncUrl = null;
    public MessageInfo(byte signalType)
    {
        this.signalType = signalType;
    }

    public MessageInfo(String conversationId,String msgId,String msgType,int contentType,String content,String fileName,long date,int dispositionReqType,String controllerPsi,String senderFuncUrl)
    {
        this.conversationId = conversationId;//Common.getDateMillSeconds();
        this.msgId = msgId;
        this.msgType = msgType;

        this.contentType = contentType;
        this.content = content;
        this.fileMetaData = fileName;

        this.date = date;
        this.dispositionReqType = dispositionReqType;
        this.controllerPsi = controllerPsi;
        this.senderFuncUrl = senderFuncUrl;
    }

    private MessageInfo(Parcel in) {
        conversationId = getReadParcelableString(in.readString());
        msgId = getReadParcelableString(in.readString());
        msgType = getReadParcelableString(in.readString());

        contentType = in.readInt();
        content = getReadParcelableString(in.readString());
        fileMetaData = getReadParcelableString(in.readString());
        date = in.readLong();
        dispositionReqType = in.readInt();

        controllerPsi = getReadParcelableString(in.readString());
        senderFuncUrl = getReadParcelableString(in.readString());
    }


    /**
     * Parcelable creator. So that it can be passed as an argument of the aidl
     * interface
     */
    public static final Creator<MessageInfo> CREATOR = new Creator<MessageInfo>() {
        public MessageInfo createFromParcel(Parcel in) {
            return new MessageInfo(in);
        }

        public MessageInfo[] newArray(int size) {
            return new MessageInfo[size];
        }
    };

    /**
     * @see Parcelable#describeContents()
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * @see Parcelable#writeToParcel(Parcel, int)
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(getWriteParcelableString(conversationId));
        dest.writeString(getWriteParcelableString(msgId));
        dest.writeString(getWriteParcelableString(msgType));

        dest.writeInt(contentType);
        dest.writeString(getWriteParcelableString(content));
        dest.writeString(getWriteParcelableString(fileMetaData));

        dest.writeLong(date);
        dest.writeInt(dispositionReqType);
        dest.writeString(getWriteParcelableString(controllerPsi));
        dest.writeString(getWriteParcelableString(senderFuncUrl));
    }


    // Yes yes that's not clean but well as for now not problem with that.
    // and we send null.
    private String getWriteParcelableString(String str) {
        return (str == null) ? "null" : str;
    }

    private String getReadParcelableString(String str) {
        return str.equalsIgnoreCase("null") ? null : str;
    }
    public long getDate() {
        return date;
    }

    public String getConversationId() {
        return conversationId;
    }

    public byte[] getConversationIdByteArr() {
        byte[] dateArr = conversationId.getBytes();
        byte[] allArray = new byte[16];
        System.arraycopy(dateArr, 0, allArray, 16-dateArr.length, dateArr.length);
        return allArray;
    }
    public byte getSignalType() {
        return signalType;
    }

    public void setMsgType(String msgType) {
        this.msgType = msgType;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setDispositionReqType(int dispositionReqType) {
        this.dispositionReqType = dispositionReqType;
    }

    public void setConversationId(String conversationId) {
        this.conversationId = conversationId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public void setDate(long date) {
        this.date = date;
    }
    public int getContentType() {
        return contentType;
    }

    public void setContentType(int contentType) {
        this.contentType = contentType;
    }

    public String getMsgId() {
        return msgId;
    }

    public String getIdStr() {
        return msgType+":"+conversationId + ":" + msgId;
    }

    public String getNotifyIdStr() {
        return SessionInfo.MsgType_Desc_Notify +":"+conversationId + ":" + msgId;
    }

    public int getDispositionReqType() {
        return dispositionReqType;
    }

    public String getContent() {
        return content;
    }

    public String getFileMetaData() {
        return fileMetaData;
    }
    public void setFileMetaData(String fileMetaData) {
        this.fileMetaData = fileMetaData;
    }

    public String getMsgType() {
        return msgType;
    }

    public byte getStatus() {
        return status;
    }

    public void setStatus(byte status) {
        this.status = status;
    }

    public void setControllerPsi(String controllerPsi) {
        this.controllerPsi = controllerPsi;
    }

    public String getControllerPsi() {
        return controllerPsi;
    }
    public boolean isSds()
    {
        if(signalType == BinaryBody.SDS_SIGNALLING_PAYLOAD)
        {
            return true;
        }
        return false;
    }
    public boolean isFd()
    {
        if(signalType == BinaryBody.FD_SIGNALLING_PAYLOAD)
        {
            return true;
        }
        return false;
    }
    public boolean isNtf()
    {
        if(signalType == BinaryBody.SDS_NOTIFYCATION
                || signalType == BinaryBody.FD_NOTIFICATION)
        {
            return true;
        }
        return false;
    }
    public String getSenderFuncUrl() {
        return senderFuncUrl;
    }
    public String toString()
    {
        StringBuffer strBuff = new StringBuffer("MessageInfo:");
        strBuff.append("signalType = ").append(signalType).append(",");
        strBuff.append("conversationId = ").append(conversationId).append(",");
        strBuff.append("msgId = ").append(msgId).append(",");
        strBuff.append("msgType = ").append(msgType).append(",");

        strBuff.append("contentType = ").append(contentType).append(",");
        strBuff.append("content = ").append(content).append(",");
        strBuff.append("fileMetaData = ").append(fileMetaData).append(",");

        strBuff.append("data = ").append(date).append(",");
        strBuff.append("status = ").append(status).append(",");
        strBuff.append("dispositionReqType = ").append(dispositionReqType).append(",");

        strBuff.append("controllerPsi = ").append(controllerPsi).append(",");
        strBuff.append("senderFuncUrl = ").append(senderFuncUrl).append(",");
        return strBuff.toString();
    }
}
